package com.schoolcloud.serve.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.common.util.UserContext;
import com.schoolcloud.serve.entity.FoundItems;
import com.schoolcloud.serve.query.FoundItemQuery;
import com.schoolcloud.serve.service.FoundItemsService;
import com.schoolcloud.serve.vo.LostFoundInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/serve/foundItems")
@RequiredArgsConstructor
public class FoundItemsController {
    private final FoundItemsService foundItemsService;

    @GetMapping("/page")
    public R<PageDTO<FoundItems>> getFoundItemsPage(FoundItemQuery query) {
        PageDTO<FoundItems> pageDTO = foundItemsService.getFoundItemsPage(query);
        return R.ok(pageDTO);
    }
    @PostMapping
    public R<?> save(@RequestBody LostFoundInfoVO foundItems) {
        foundItems.setUserId(UserContext.getUserToInteger());
        foundItemsService.saveFoundItems(foundItems);
        return R.ok().setMsg("保存成功");
    }
    @PutMapping
    public R<?> update(@RequestBody LostFoundInfoVO foundItems) {
        foundItems.setUserId(UserContext.getUserToInteger());
        foundItemsService.saveFoundItems(foundItems);
        return R.ok().setMsg("更新成功");
    }
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        foundItemsService.removeById(id);
        return R.ok();
    }
    @GetMapping("/{id}")
    public R<LostFoundInfoVO> getById(@PathVariable Integer id) {
        LostFoundInfoVO foundItems = foundItemsService.getFoundInfoByFoundId(id);
        return R.ok(foundItems);
    }
}
